package com.hqd.ch03.bean.jdbc.v29;


import com.hqd.ch03.bean.jdbc.User;
import com.hqd.ch03.v29.aop.framework.AopContext;
import com.hqd.ch03.v29.jdbc.core.SimpleJdbcTemplate;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.sql.SQLException;
import java.util.List;

@Data
@NoArgsConstructor
public class UserServiceImplV29 {
    private SimpleJdbcTemplate jdbcTemplate;

    public void updateData(User user) throws SQLException {
        jdbcTemplate.upate("UPDATE user SET name = ?,age=?,sex=? WHERE id = ?", user.getName(), user.getAge(), user.getSex(),
                user.getId());
        ((UserServiceImplV29) AopContext.currentProxy()).deleteData(28);
    }

    public void deleteData(Integer id) throws SQLException {
        jdbcTemplate.upate("DELETE FROM user WHERE id =?", id);
    }

    public void saveData(User user) throws SQLException {
        jdbcTemplate.upate("insert into user(id,name,age,sex) values (?,?,?,?)",
                user.getId(), user.getName(), user.getAge(), user.getSex());
        ((UserServiceImplV29) AopContext.currentProxy()).deleteData(user.getId());
    }

    public List<User> getData() throws Exception {
        return jdbcTemplate.queryForClass("select *from user", User.class);
    }

    public String toString() {
        return "";
    }
}
